#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
  int pipe_fd[2];
  pid_t pid;
  char message[] = "hello Pipe!";
  char buffer[100];

  //创建匿名管道
  if (pipe(pipe_fd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }

  if (fork() == 0) { //子进程写入消息到管道
    // sleep(10);
    write(pipe_fd[1], message, strlen(message) + 1);

    close(pipe_fd[1]); //关闭管道
  } else {
    printf("Parent:Prepare to read\n");
    read(pipe_fd[0], buffer, sizeof(buffer));
    close(pipe_fd[0]); //关闭读取端
    printf("Parent:recesive message:%s\n", buffer);
    wait(NULL);
  }
}
